<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑广告</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <#assign base=request.contextPath  />
    <link rel="stylesheet" href="${base}/frame/layui/css/layui.css?t=1534970980648" media="all">
    <script type="text/javascript" src="${base}/js/zhang.js"></script>
    <script src="${base}/frame/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${base}/js/ztree/jquery-1.4.4.min.js"></script>
    <script src="${base}/js/jquery.min.js"></script>
    <script type="text/javascript" src="${base}/wangEdit/wangEditor.min.js"></script>
    <style type="text/css">
        form {
            padding: 20px;
        }

        .layui-btn-radius {
            border-radius: 5px;
        }

        .layui-table-cell {
            height: auto;
            white-space: normal;
        }
    </style>
</head>
<body>
<form class="layui-form" action="">

    <div class="layui-form-item">
        <label class="layui-form-label">选择类型</label>
        <div class="layui-input-block">
            <select name="interest" id="flag"  lay-filter="aihao">
                <option value="0">请选择</option>
                <option value="1">图文</option>
                <option value="2">图文连接</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">广告图</label>
        <div class="layui-upload-drag" id="test10">
            <div id="imgUrl">
                <img src="${advertisement.url}" style="width: 342px;height: 136px">
            </div>
            <i class="layui-icon"></i>
            <p>点击上传，或将文件拖拽到此处</p>
        </div>
    </div>




<div id="abc">
</div>
    <#if advertisement.flag==2>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">内容</label>
                    <div class="layui-inline">
                        <div id="content" style="height: 324px;width:730px;">
                        </div>
                    </div>
                </div>
            </div>


    </#if>
     <#if advertisement.flag==1>

     </#if>





    <div class="layui-form-item" style="text-align:center">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-radius" lay-submit lay-filter="formDemo" type="button" id="aa"
                    onclick="onSubmit()" style="background-color: #0092EF;">立即提交
            </button>
            <#--<button type="reset" class="layui-btn layui-btn-primary layui-btn-radius">重置</button>-->
        </div>
    </div>
</form>
<script>


</script>
<script>
    var E = window.wangEditor;
    var content = new E('#content');
    content.create();
    var text = content.txt.html(`${advertisement.content}`);

    var url;
    layui.use(['form', 'layedit', 'laydate', 'layer','upload'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , upload = layui.upload
            , laydate = layui.laydate;

        //查询下拉框
        $.ajax({
            type: "GET",
            url: "${base}/admin/advertisement/selectByIdAdvertisement",
            data: {id:'${advertisement.id}'},
            success: function (data) {

                if(data.flag==1){
                    $("#flag").find("option[value='" + 1 + "']").attr("selected", true);
                }
                if(data.flag==2){
                    $("#flag").find("option[value='" + 2 + "']").attr("selected", true);
                }
                form.render('select');

            }
        });


        //拖拽上传
        upload.render({
            elem: '#test10'
            ,url: '${base}/admin/file/uploadFile'
            ,done: function(res){
                layer.closeAll('loading');
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                url = res.data
                $("#imgUrl").html("")
                $("#imgUrl").append(
                    '<img src="'+res.data+'" style="width: 342px;height: 136px" /> '
                )
                return layer.msg("上传成功")
            }
        });

        form.on('select(aihao)', function(data){
            //console.log(data.elem); //得到select原始DOM对象
            //console.log(data.value); //得到被选中的值
            //console.log(data.othis); //得到美化后的DOM对象
            if(data.value==1){
                $("#abc").html("")
                $("#abc").append("<p></p>")
            }
            if(data.value==2){
                // $("#abc").html("")
                $("#abc").append(
                '<div class="layui-form-item">'+
                '<div class="layui-inline">'+
                '<label class="layui-form-label">内容</label>'+
                '<div class="layui-inline">'+
                '<div id="content" style="height: 324px;width:730px;">'+
                '</div>'+
                '</div>'+
                '</div>'+
                '</div>'
                )
            }
            //渲染下文本域
            abc();
        });




    })
</script>
<script>

    var content;
    function abc() {
        var E = window.wangEditor;
         content = new E('#content');
        content.create();
    }


    //添加
    function onSubmit() {
        var jsonData = {};
        jsonData.url = url;
       if($("#flag").val()==2){
           jsonData.content = content.txt.html();
       }
        if($("#flag").val()==1){
            jsonData.content = "1";
        }

        jsonData.flag = $("#flag").val();
        jsonData.id = '${advertisement.id}';

        $.ajax({
            type: "POST",
            contentType: "application/json;charset=UTF-8",
            url: "${base}/admin/advertisement/updateByIdAdvertisement",
            data: JSON.stringify(jsonData),
            success: function (data) {
                if(data.code===200){
                    layer.msg(data.msg);
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    }, 300)
                    var table = parent.layui.table;
                    //表刷新方法
                    table.reload('testReload', {
                        url: '${base}/admin/advertisement/selectMyPage'
                        , where: {} //设定异步数据接口的额外参数
                        //,height: 300
                    });
                }else{
                    layer.msg(data.msg)
                }

            }
        });
    }

</script>

</body>
</html>
